import Taro from '@tarojs/taro';
import {reqHead, reqUrl, reqType, reqTip} from '../config'

// 请求封装
const http = (url, resData, loading) => new Promise((resolve, reject) => {
  // 是否开启loading
  loading && Taro.showLoading({
    title: reqTip
  })

  // 接口访问
  Taro.request({
    url: `${reqUrl}${url}`,
    data: resData,
    method: reqType,
    header:{
      ...reqHead,
      Accept: 'application/json',
      Authorization: '222'
    }
  })
  .then(res => {
    let dataObj = res.data
    const {code, data} = dataObj;
    // code判断 
    if (code === 200) {
      resolve(data)
    }else {
      reject(dataObj)
    }
    // 关闭loaidng
    loading && Taro.hideLoading()
  })
  .catch(err => {
    reject(err)
    // 关闭loaidng
    loading && Taro.hideLoading()
  })

  // 请求

  
})

export default http;